﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.Slaughter;
using Tb.App.Domain.Rabbit.Slaughter;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Slaughter
{
    /// <summary>
    /// 屠宰信息查询-命令处理
    /// </summary>
    public class GetSlaughterQueryCommandHandler : IRequestHandler<GetSlaughterQueryCommand, ApiResult<SlaughterModel>>
    {
        private IBaseRepository<SlaughterModel> slaughterRep;

        public GetSlaughterQueryCommandHandler(IBaseRepository<SlaughterModel> slaughterRep)
        {
            this.slaughterRep = slaughterRep;
        }
        public async Task<ApiResult<SlaughterModel>> Handle(GetSlaughterQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<SlaughterModel> result=new ApiResult<SlaughterModel>();
            var list =await slaughterRep.GetAll().Where(x => !x.IsDel && x.Batch == request.Batch).OrderByDescending(x => x.Id).FirstOrDefaultAsync();
            if (list == null) 
            {
                result.Code = ApiEnum.查询失败;
                result.Message = ApiEnum.查询失败.ToString();
            }
            else
            {
                result.Code = ApiEnum.查询成功;
                result.Message = ApiEnum.查询成功.ToString();
                result.Data = list;
            }
            return result;

        }
    }
}
